{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mongabay basemaps and layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipyleaflet as ipyl\n",
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basemaps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "mongabay_paper_url = 'https://api.mapbox.com/styles/v1/mongabay/ckai1bze50hmj1ipohkupl0qo/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoibW9uZ2FiYXkiLCJhIjoiY2s4N25oZG82MGFyejNsb2lnN3ZrbG1jbyJ9.coW3do99wi_PmnW6OylFbA'\n",
    "#'https://api.mapbox.com/styles/v1/mongabay/ck8egahvy26m01hnv9f7i1cf3/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoibW9uZ2FiYXkiLCJhIjoiY2s4N25oZG82MGFyejNsb2lnN3ZrbG1jbyJ9.coW3do99wi_PmnW6OylFbA'\n",
    "mongabay_paper_basemap = ipyl.TileLayer(\n",
    "    url=topography_url,\n",
    "    layers='topography',\n",
    "    format='image/png',\n",
    "    name='Topography',\n",
    "    opacity=1\n",
    ")\n",
    "\n",
    "\n",
    "planet_url = 'https://tiles.planet.com/basemaps/v1/planet-tiles/global_monthly_2020_01_mosaic/gmap/{z}/{x}/{y}.png?api_key=f366791cb438466fb9e3e492e721da8d'\n",
    "planet_basemap = ipyl.TileLayer(\n",
    "    url=planet_url ,\n",
    "    layers='planet',\n",
    "    format='image/png',\n",
    "    name='Planet',\n",
    "    opacity=1\n",
    ")\n",
    "\n",
    "landsat_url = 'https://storage.cloud.google.com/skydipper-landsat-cache/2018/{z}/{x}/{y}.png'\n",
    "landsat_basemap = ipyl.TileLayer(\n",
    "    url=landsat_url,\n",
    "    layers='landsat',\n",
    "    format='image/png',\n",
    "    name='Landsat',\n",
    "    opacity=0.5\n",
    ")\n",
    "\n",
    "canopy_height_url = 'https://api.mapbox.com/v4/mongabay.0rgyb3bx/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibW9uZ2FiYXkiLCJhIjoiY2s4N25oZG82MGFyejNsb2lnN3ZrbG1jbyJ9.coW3do99wi_PmnW6OylFbA'\n",
    "canopy_height_layer = ipyl.TileLayer(\n",
    "    url=canopy_height_url,\n",
    "    layers='canopy_height',\n",
    "    format='image/png',\n",
    "    name='Global Forest Canopy Height',\n",
    "    opacity=1\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[\n",
    "  {\n",
    "    \"id\": \"gain\",\n",
    "    \"name\": \"Forest canopy height\",\n",
    "    \"config\": {\n",
    "      \"type\": \"raster\",\n",
    "      \"source\": {\n",
    "        \"type\": \"raster\",\n",
    "        \"tiles\": [\n",
    "          \"https://api.mapbox.com/v4/mongabay.0rgyb3bx/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibW9uZ2FiYXkiLCJhIjoiY2s4N25oZG82MGFyejNsb2lnN3ZrbG1jbyJ9.coW3do99wi_PmnW6OylFbA\"\n",
    "        ],\n",
    "        \"minzoom\": 3,\n",
    "        \"maxzoom\": 12\n",
    "      }\n",
    "    },\n",
    "    \"legendConfig\": {\n",
    "      \"items\": [\n",
    "        {\n",
    "          \"color\": \"#ffffff\",\n",
    "          \"value\": \"0\"\n",
    "        },\n",
    "        {\n",
    "          \"color\": \"#E7F1DB\",\n",
    "          \"value\": \"5\"\n",
    "        },\n",
    "        {\n",
    "          \"color\": \"#C2DCA3\",\n",
    "          \"value\": \"10\"\n",
    "        },\n",
    "        {\n",
    "          \"color\": \"#9EC76E\",\n",
    "          \"value\": \"20\"\n",
    "        },\n",
    "        {\n",
    "          \"color\": \"#79B137\",\n",
    "          \"value\": \"35\"\n",
    "        },\n",
    "        {\n",
    "          \"color\": \"#559C01\",\n",
    "          \"value\": \"50 m\"\n",
    "        }\n",
    "        ],\n",
    "          \"type\": \"gradient\",\n",
    "          \"unit\": \"m\"\n",
    "        }\n",
    "  }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e609fdce78fd4bde8ed2f7383a764928",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(center=[0, 0], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_out_text'…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "center = (0, 0)\n",
    "m = ipyl.Map(center=(center), zoom=2)\n",
    "\n",
    "\n",
    "m.add_layer(mongabay_paper_basemap)\n",
    "m.add_layer(planet_basemap)\n",
    "m.add_layer(landsat_basemap)\n",
    "m.add_layer(canopy_height_layer)\n",
    "\n",
    "control = ipyl.LayersControl(position='topright')\n",
    "m.add_control(control)\n",
    "m.add_control(ipyl.FullScreenControl())\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Global Forest Height**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "name='Global Forest Height'\n",
    "\n",
    "image = ee.Image('users/iker/Mongabay/3DGlobalVeg')\n",
    "\n",
    "hansenImage = ee.Image('UMD/hansen/global_forest_change_2015')\n",
    "\n",
    "# Select the land/water mask.\n",
    "datamask = hansenImage.select('datamask')\n",
    "\n",
    "# Create a binary mask.\n",
    "mask = datamask.eq(1)\n",
    "\n",
    "# Update the image with the water mask.\n",
    "image = image.updateMask(mask)\n",
    "\n",
    "# Make a water image out of the mask.\n",
    "water = mask.Not()\n",
    "\n",
    "# Mask water with itself to mask all the zeros (non-water)..\n",
    "water = water.mask(water)\n",
    "\n",
    "sld_interval = '<RasterSymbolizer>' + \\\n",
    "'<ColorMap extended=\"false\" type=\"ramp\">' + \\\n",
    "'<ColorMapEntry color=\"#ffffff\" quantity=\"0\"  opacity=\"0\" />' + \\\n",
    "'<ColorMapEntry color=\"#E7F1DB\" quantity=\"5\"  opacity=\"1\" />' + \\\n",
    "'<ColorMapEntry color=\"#C2DCA3\" quantity=\"10\"  />' + \\\n",
    "'<ColorMapEntry color=\"#9EC76E\" quantity=\"20\" />' + \\\n",
    "'<ColorMapEntry color=\"#79B137\" quantity=\"35\"  />' + \\\n",
    "'<ColorMapEntry color=\"#559C01\" quantity=\"50\"  />' + \\\n",
    "'</ColorMap>' + '</RasterSymbolizer>'\n",
    "\n",
    "# Make a mosaic of images.\n",
    "mosaic = ee.ImageCollection([\n",
    "  image.sldStyle(sld_interval)\n",
    "  #water.visualize(palette = '000044'),\n",
    "]).mosaic()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9c28e8d9a1814fdc89d71082abb081a1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(center=[0, 0], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_out_text'…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ee_tiles = 'https://earthengine.googleapis.com/map/{mapid}/{{z}}/{{x}}/{{y}}?token={token}' \n",
    "\n",
    "m = ipyl.Map(center=(0,0), zoom=2)\n",
    "\n",
    "mapid = mosaic.getMapId()\n",
    "tiles_url = ee_tiles.format(**mapid)\n",
    "\n",
    "tile_layer = ipyl.TileLayer(\n",
    "    url=tiles_url,\n",
    "    format='image/png',\n",
    "    name=name,\n",
    "    opacity=1\n",
    ")\n",
    "\n",
    "m.add_layer(topography_basemap)\n",
    "m.add_layer(tile_layer)\n",
    "    \n",
    "control = ipyl.LayersControl(position='topright')\n",
    "m.add_control(control)\n",
    "m.add_control(ipyl.FullScreenControl())\n",
    "    \n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_tiles(images, sld_intervals, names, basemap='topography', center = (0, 0), zoom=2):\n",
    "    ee_tiles = 'https://earthengine.googleapis.com/map/{mapid}/{{z}}/{{x}}/{{y}}?token={token}' \n",
    "    \n",
    "    m = ipyl.Map(center=(center), zoom=zoom)\n",
    "    \n",
    "    if basemap == 'topography':\n",
    "        m.add_layer(topography_basemap)\n",
    "    if basemap == 'landsat':\n",
    "        m.add_layer(landsat_basemap)\n",
    "        \n",
    "    for n, image in enumerate(images):\n",
    "        image = image.sldStyle(sld_intervals[n])\n",
    "        mapid = image.getMapId()\n",
    "        tiles_url = ee_tiles.format(**mapid)\n",
    "        \n",
    "        tile_layer = ipyl.TileLayer(\n",
    "            url=tiles_url,\n",
    "            format='image/png',\n",
    "            name=names[n],\n",
    "            opacity=1\n",
    "        )\n",
    "    \n",
    "        m.add_layer(tile_layer)\n",
    "    \n",
    "    control = ipyl.LayersControl(position='topright')\n",
    "    m.add_control(control)\n",
    "    m.add_control(ipyl.FullScreenControl())\n",
    "    \n",
    "    return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
